//
//  HHPWebViewController.m
//  HHPLiveVideo
//
//  Created by 韩红坡 on 2017/3/17.
//  Copyright © 2017年 韩红坡. All rights reserved.
//

#import "HHPWebViewController.h"
#import <WebKit/WebKit.h>

@interface HHPWebViewController () <WKNavigationDelegate, WKUIDelegate>

@property (nonatomic, strong) WKWebView *wkWebView;
@property (nonatomic, strong) UIProgressView *progressView;

@property (nonatomic, copy) NSString *baseUrl;

@end

@implementation HHPWebViewController
#pragma mark - life cycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initViews];
    
    [self.wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)dealloc {
    [self.wkWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}

#pragma mark - private methods
- (void)initViews
{
    [self.view addSubview:self.wkWebView];
    [self.view addSubview:self.progressView];
}

#pragma mark - load html
- (void)loadHtmlString:(NSString *)htmlString withTitle:(NSString *)title
{
    self.title = title;
    
    NSURL *url = [NSURL URLWithString:[self.baseUrl stringByAppendingString:htmlString]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.timeoutInterval = 15.0f;
    
    [self.wkWebView loadRequest:request];
}

- (void)loadAbsoluteHtmlString:(NSString *)htmlString withTitle:(NSString *)title
{
    self.title = title;
    
    NSURL *url = [NSURL URLWithString:htmlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.timeoutInterval = 15.0f;
    
    [self.wkWebView loadRequest:request];
}

#pragma mark - response envents
/*
 *在监听方法中获取网页加载的进度，并将进度赋给progressView.progress
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.wkWebView.estimatedProgress;
        
        if (self.progressView.progress == 1) {
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark - WKWKNavigationDelegate
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    self.progressView.hidden = NO;
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.1f);
    [self.view bringSubviewToFront:self.progressView];
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
}

//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    self.progressView.hidden = YES;
    [MBProgressHUD showInfo:@"网络异常"];
}

#pragma mark - lazy
- (WKWebView *)wkWebView
{
    if (!_wkWebView) {
        WKWebViewConfiguration *wkConfig = [[WKWebViewConfiguration alloc] init];
        wkConfig.allowsInlineMediaPlayback = YES;
        wkConfig.allowsPictureInPictureMediaPlayback = YES;
        
        _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64) configuration:wkConfig];
        _wkWebView.navigationDelegate = self;
        _wkWebView.backgroundColor = ViewBgColor;
    }
    return _wkWebView;
}

- (NSString *)baseUrl
{
    if (!_baseUrl) {
//        _baseUrl = [[ConfigManager selectDicJsonWithKey:@"MyfangH5Pages"][0] name];
        _baseUrl = @"";
    }
    
    return _baseUrl;
}

- (UIProgressView *)progressView
{
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        _progressView.progressTintColor = RGB(20, 120, 227);
        _progressView.trackTintColor = [UIColor clearColor];
        _progressView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
    }
    
    return _progressView;
}

@end
